from .common import *

class ArgsEditDialog(wx.Dialog):
    
    def __init__(self, parent, labels: List[str], context, **kwargs):
        wx.Dialog.__init__(self, parent, id = wx.ID_ANY, title = f"路由参数填写", size=(480, 350))
        self.labels = labels
        self.context: Dict[str, Any] = context
        self.data = []

        self._init_UI()

        # self.Bind(wx.EVT_CLOSE, self.onClose)

    def _init_UI(self):
        """初始化界面布局"""
        self.panel = wx.Panel(self)
        self.panelSizer = wx.BoxSizer(wx.VERTICAL)
        self.panel.SetSizer(self.panelSizer)

        '''
            使用滚动面板
        '''
        self.scollPanel = scrolledpanel.ScrolledPanel(self.panel, -1) # 定义滚动面板
        self.scollPanel.SetupScrolling() # 开启滚动
        scollPanelSizer = wx.BoxSizer(wx.VERTICAL)
        self.scollPanel.SetSizer(scollPanelSizer)
        self.panelSizer.Add(self.scollPanel, 3, wx.EXPAND | wx.ALL, 2)

        self.flexGridSizer = wx.FlexGridSizer(cols=3, hgap=5, vgap=5)
        scollPanelSizer.Add(self.flexGridSizer, 1, wx.EXPAND | wx.ALL, 5)
        self.flexGridSizer.AddGrowableCol(1) 

        for label in self.labels:
            self.data.append(
                (
                    wx.StaticText(self.scollPanel, -1, f'类型：{label[0]}'), 
                    wx.StaticText(self.scollPanel, -1, label[1]), 
                    wx.TextCtrl(self.scollPanel, -1, value='')
                )
            )
        
        for _ in self.data:
            self.flexGridSizer.Add(_[1], 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL)
            self.flexGridSizer.Add(_[2], 0, wx.EXPAND)
            self.flexGridSizer.Add(_[0], 0, wx.EXPAND)

        self.btnSubmit = buttons.GenButton(self.scollPanel, -1, label='提交')
        scollPanelSizer.Add(self.btnSubmit, 0, wx.EXPAND | wx.ALL, 2)
        self.Bind(wx.EVT_BUTTON, self.onBtnSubmit, self.btnSubmit)

    def onBtnSubmit(self, evt):
        """提交"""
        for d in self.data:
            k = d[1].GetLabelText()
            w = d[2].GetValue().strip()
            if not w:
                RichMsgDialog.showOkMsgDialog(self, "参数必须全部填写", "错误")
                self.context.clear()
                return
            self.context[k] = w
        self.Close()

    # def onClose(self, evt: wx.CloseEvent):
    #     """关闭窗口"""
    #     if not self.context:
    #         if not RichMsgDialog.showYesNoMsgDialog(self, '参数残缺，无法打开网页，确认退出？', '警告'):
    #             evt.Veto()
    #             return
    #     self.Destroy()
    #     # self.Close()

def showArgsEditDialog(parent, labels: List[str]):
    context = {}
    dlg = ArgsEditDialog(parent, labels, context)
    dlg.ShowModal()
    dlg.Close(True)
    return context
